//
//  MBProfileInterestsView.m
//  Meboo
//
//  Created by kyo on 2024/3/13.
//

#import "MBProfileInterestsView.h"
#import "MBInterestsCell.h"
#import "TightnessFlowLayout.h"
#import "NSString+Extension.h"
@interface MBProfileInterestsView ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *titleLab;
@end
@implementation MBProfileInterestsView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self getData];
    }
    return self;
}

#pragma mark - Private Method
- (void)getData {
    __WeakSelf
    [[MBMineDataManager  sharedManager] getInterests:^(BOOL success) {
        if (success) {
            if ([MBMineDataManager  sharedManager].interestsArr.count < 19) {
                weakSelf.collectionView.scrollEnabled = NO;
            }
            [weakSelf.collectionView reloadData];
        }
    }];
}

#pragma mark - Action

#pragma mark -- collectionView  delegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [MBMineDataManager  sharedManager].interestsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MBInterestsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MBInterestsCell" forIndexPath:indexPath];
    cell.model = [MBMineDataManager  sharedManager].interestsArr[indexPath.row];
    return cell;
}

//设置每个section的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(MBHeightScale(0), MBWidthScale(0), MBHeightScale(0), MBWidthScale(0));
}

//设置列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return MBWidthScale(6);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return MBHeightScale(6);
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    return CGSizeMake((ScreenWidth-MBWidthScale(72))/3, MBHeightScale(37));
    NSString *titleStr = [MBMineDataManager  sharedManager].interestsArr[indexPath.row].interest;
    //此方法根据文本内容和字号生成size
    CGSize size = [titleStr sizeWithFont:MBHelFontSizeScale(15) maxSize:CGSizeMake(ScreenWidth-MBWidthScale(50), MBHeightScale(28))];
    return CGSizeMake(size.width+MBWidthScale(20), MBHeightScale(28));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    KLInterestsModel *model = [MBMineDataManager  sharedManager].interestsArr[indexPath.row];
    NSInteger num = 0;
    for (KLInterestsModel *model0 in [MBMineDataManager  sharedManager].interestsArr) {
        if (model0.selected) {
            num++;
        }
    }
    if (num == 8 && !model.selected) {
        [MBProgressHUD showInfoMessage:@"You can select up to 8 at most!"];
        return;
    }
    model.selected = !model.selected;
    [[MBMineDataManager  sharedManager].interestsArr replaceObjectAtIndex:indexPath.row withObject:model];
    [self.collectionView reloadData];
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
    }];
    
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.equalTo(self);
        make.top.mas_equalTo(MBHeightScale(32));
    }];
}

#pragma mark - Setter && Getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = PDSemiBoldScale(18);
        _titleLab.textColor = HEXS(@"333333");
        _titleLab.text = @"interests";
    }
    return _titleLab;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        TightnessFlowLayout *flowLayout = [[TightnessFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[MBInterestsCell class] forCellWithReuseIdentifier:@"MBInterestsCell"];
    }
    return _collectionView;
}
@end
